﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPFOpenVideo.Bll;
using System.IO.Ports;
using WPFOpenVideo.Att;

namespace WPFOpenVideo
{
    /// <summary>
    /// SettingPort.xaml 的交互逻辑
    /// </summary>
    public partial class SettingPort : Window
    {


        //↓↓↓↓↓↓↓↓↓↓↓↓↓↓公共申明↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
        private SettingPortBll NewSettingPortBll;
        private TaskFactory ModifyPortParamTask;
        private static SettingPort _FormSettingPort=null;
        AttributeFactory AttFactory;

        //串口对象字段申明
        private string _portName;
        private int _baudRate;
        private int _dataBite;
        private StopBits _stopBite;
        private Parity _parity;
        //↑↑↑↑↑↑↑↑↑↑↑↑↑↑公共申明↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//

        private SettingPort() 
        {
            InitializeComponent();
            NewSettingPortBll = new SettingPortBll();
            NewSettingPortBll.SetCombox(Combox2);
            NewSettingPortBll.SetCombox2(Combox3);

            //combobox默认值
            Combox1.Text = NewSettingPortBll.PortName;
            Combox2.SelectedValue = NewSettingPortBll.StopBite;
            Combox3.SelectedValue = NewSettingPortBll.Parity;
            TextBox1.Text = NewSettingPortBll.BaudRate.ToString();
            TextBox2.Text = NewSettingPortBll.DataBite.ToString();

            //线程创建
            ModifyPortParamTask = new TaskFactory();

            //创建工厂实例
            AttFactory = new AttributeFactory();
        }

        public static SettingPort FormSettingPort()
        {
            if (_FormSettingPort == null)
            {
                _FormSettingPort = new SettingPort();
            }

            return _FormSettingPort;
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string Path = System.Windows.Forms.Application.StartupPath + "Config\\SerialPortSetting.ini";
            Console.WriteLine(Path);
        }


        [IsConfirmModify("确认修改吗？")]
        private void ModifyPort()
        {
            Console.WriteLine($"该线程是{Thread.CurrentThread.ManagedThreadId}");
            NewSettingPortBll.PortName = _portName;
            NewSettingPortBll.StopBite = _stopBite;
            NewSettingPortBll.Parity = _parity;
            NewSettingPortBll.BaudRate = _baudRate;
            NewSettingPortBll.DataBite = _dataBite;
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            ModifyField();
            //ModifyPortParamTask.StartNew(() => { ModifyPort(); });

            //利用工厂反射
            ModifyPortParamTask.StartNew(() =>
            {
                AttFactory.IsConfirmModifyBll("ModifyPort", _FormSettingPort,"修改成功！");
            });

        }

        private void ModifyField()
        {
            _portName= Combox1.Text;
            _stopBite = (StopBits)Combox2.SelectedValue;
            _parity= (Parity)Combox3.SelectedValue;
            _parity= (Parity)Combox3.SelectedValue;
            _baudRate= Convert.ToInt32(TextBox1.Text);
            _dataBite= Convert.ToInt32(TextBox2.Text);
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            _FormSettingPort = null;
        }
    }
}
